package com.jgsu.farm.service.impl;

import com.jgsu.farm.entity.Information;
import com.jgsu.farm.entity.MatchImage;
import com.jgsu.farm.entity.User;
import com.jgsu.farm.entity.Video;
import com.jgsu.farm.exception.DataAddException;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.mapper.InformationMapper;
import com.jgsu.farm.mapper.UserMapper;
import com.jgsu.farm.mapper.VideoMapper;
import com.jgsu.farm.mapper.sql.MatchImageMapperSql;
import com.jgsu.farm.service.InformationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InformationServiceImpl implements InformationService {
    @Autowired
    InformationMapper informationMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    MatchImageMapperSql matchImageMapperSql;
    @Autowired
    VideoMapper videoMapper;

    @Override
    public void add(Information information) {
        if (information.getAuthor() == null || information.getImages() == null || information.getVideos() == null) {
            informationMapper.insert(information);
        } else {
            User userId = userMapper.selectByPrimaryKey(information.getAuthor());
            List<MatchImage> matchImageList = matchImageMapperSql.selectByMatchId(information.getImages());
            Video video = videoMapper.selectByPrimaryKey(information.getVideos());
            if (userId != null && !matchImageList.isEmpty() && video != null) {
                informationMapper.insert(information);
            } else {
                throw new DataAddException("604", "information数据插入异常");
            }
        }
    }

    @Override
    public void delete(Integer infoid) {
        Integer infoId = informationMapper.deleteByPrimaryKey(infoid);
        if (infoId == 0 || infoId == null) {
            throw new DataNotExistException("603", "不存在该id,胁迫情报删除失败");
        }
    }

    @Override
    public PageInfo<Information> selectAll(int page, int pagesize) {
        PageHelper.startPage(page, pagesize);
        List<Information> information = informationMapper.selectAll();
        PageInfo<Information> pageInfoInformationList = new PageInfo<Information>(information);
        return pageInfoInformationList;
    }


    @Override
    public void update(Information information) {
        Information informationId = informationMapper.selectByPrimaryKey(information.getInfoid());
        if (informationId != null) {
            if (information.getAuthor() == null || information.getImages() == null || information.getVideos() == null) {
                informationMapper.updateByPrimaryKey(information);
            } else {
                User userId = userMapper.selectByPrimaryKey(information.getAuthor());
                List<MatchImage> matchImageList = matchImageMapperSql.selectByMatchId(information.getImages());
                Video video = videoMapper.selectByPrimaryKey(information.getVideos());
                if (userId != null && !matchImageList.isEmpty() && video != null) {
                    informationMapper.updateByPrimaryKey(information);
                } else {
                    throw new DataNotExistException("604", "information数据更新异常");
                }
            }
        } else {
            throw new DataNotExistException("604", "information数据的infoid不存在，更新异常");
        }
    }
}
